package org.zdc.pivot.wtk;
import haxe.Json;
import nme.geom.Point;
import org.zdc.core.IllegalArgumentException;

class Dimensions
{
	private var _width:Float;
	private var _height:Float;
	
	public static inline var WIDTH_KEY = "width";
	public static inline var HEIGHT_KEY = "height";
	
	public function new(width:Float, height:Float) 
	{
		this.width = width;
		this.height = height;
	}
	
	public static function make(obj:Dynamic):Dimensions {
		var w = 0;
		var h = 0;
		
		if (obj != null) {
			var hasWid = Reflect.hasField(obj, WIDTH_KEY);
			var hasHei = Reflect.hasField(obj, HEIGHT_KEY);
			if (hasWid) {
				w = Reflect.getProperty(obj, WIDTH_KEY);
			}
			if (hasHei) {
				h = Reflect.getProperty(obj, HEIGHT_KEY);
			}
		}
		
		return new Dimensions(w, h);
	}
	
	public static function decode(value:String):Dimensions {
		if (value == null) {
			throw new IllegalArgumentException();
		}
		
		return make(Json.parse(value));
	}
	
	public function equals(object:Dynamic):Bool {
		var eq = false;
		
		if (Std.is(object, Dimensions)) {
			var obj = cast(object, Dimensions);
			eq = (obj.width == width && obj.height == height);
		}
		
		return eq;
	}
	
	private function get_width():Float 
	{
		return _width;
	}
	
	private function set_width(value:Float):Float 
	{
		return _width = value;
	}
	
	public var width(get_width, set_width):Float;
	
	private function get_height():Float 
	{
		return _height;
	}
	
	private function set_height(value:Float):Float 
	{
		return _height = value;
	}
	
	public var height(get_height, set_height):Float;
}